<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Devices</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='control_style.css') }}"/>
</head>
<body>
<div id="" class="left full-height">
    <!-- logo -->
    <div id="" class="logo-bar">
        <div id="" class="ele full-height">
            <h1 style="color: #ededed;">LOGO</h1>
        </div>

    </div>
    <!-- tabs -->
    <div id="" class="tab-bar">
        <div style="height: 50px;width: 100%"></div>
        <a href="/devices">
            <div id="" class="tab-content no-choix">
                DEVICES
            </div>
        </a>
        <a href="/types">
            <div id="" class="tab-content no-choix">
                TYPE
            </div>
        </a>
        <a href="/conn">
            <div id="" class="tab-content choix">
                CONNECTIONS
            </div>
        </a>
        <a href="/log">
            <div id="" class="tab-content no-choix">
                LOG
            </div>
        </a>
        <a href="/groups">
            <div id="" class="tab-content no-choix">
                GROUPS
            </div>
        </a>
    </div>
</div>

<div id="" class="right full-height">
    <!-- admin -->
    <div id="" class="admin-bar">

    </div>
    <!-- main content -->
    <div id="" class="content-background">
        <div class="content-box">
            <div id="" class="content-control-box">
                <button type="button" class="btn" onclick="jump('/add_conn')">ADD</button>
                <button type="button" class="btn" onclick="diagram_show()">RELATIONS</button>
            </div>
            <div id="" class="data-box" style="overflow-y: auto;">
                <table id="table-connection" class="table_style" cellspacing=0>
                    <tr>
                        <th>id</th>
                        <th>device</th>
                        <th>related_device</th>
                        <th>related_group</th>
                        <th>button</th>
                    </tr>
                    {% for connection in connections %}
                        <tr>
                            <td>{{ connection[0] }}</td>
                            <td>{{ devices_dict[connection[1]] }}</td>
                            {% if connection[2]=="DEVICE" %}
                                <td>{{ devices_dict[connection[3]] }}</td>
                                <td></td>
                                <td>
                                    <button class="btn-small"
                                            onclick="jump('/modify_conn/{{ connection[0] }}')">
                                        modify
                                    </button>
                                    <button class="btn-small" onclick="confirm_delete_connection({{ connection[0] }})">
                                        delete
                                    </button>
                                </td>
                            {% else %}
                                <td></td>
                                <td>{{ gd[connection[4]] }}</td>
                                <td>
                                    <button class="btn-small"
                                            onclick="jump('/modify_conn/{{ connection[0] }}')">
                                        modify
                                    </button>
                                    <button class="btn-small" onclick="confirm_delete_connection({{ connection[0] }})">
                                        delete
                                    </button>
                                </td>
                            {% endif %}
                        </tr>
                    {% endfor %}
                </table>

            </div>
        </div>
    </div>
</div>
</body>
<script>
    function jump(url) {
        window.location.href = url;
    }

    function create_xhr() {
        let XHR = [  //兼容不同浏览器和版本得创建函数数组
            function () {
                return new XMLHttpRequest()
            },
            function () {
                return new ActiveXObject("Msxml2.XMLHTTP")
            },
            function () {
                return new ActiveXObject("Msxml3.XMLHTTP")
            },
            function () {
                return new ActiveXObject("Microsoft.XMLHTTP")
            }
        ];
        let xhr = null;
        //尝试调用函数，如果成功则返回XMLHttpRequest对象，否则继续尝试
        for (let i = 0; i < XHR.length; i++) {
            try {
                xhr = XHR[i]();
            } catch (e) {
                continue  //如果发生异常，则继续下一个函数调用
            }
            break;  //如果成功，则中止循环
        }
        return xhr;  //返回对象实例
    }

    function confirm_delete_connection(connection_id) {
        let msg = "您真的确认要删除绑定关系" + connection_id + "吗？"
        let command = {
            "id": connection_id
        }
        if (confirm(msg) == true) {
            let xhr = create_xhr();
            xhr.open("POST", "/equipmentType/deleteEquipmentConnection", true)
            xhr.onreadystatechange = function () {  //绑定响应状态事件监听函数
                if (xhr.readyState == 4) {  //监听readyState状态
                    if (xhr.status == 200 || xhr.status == 0) {  //监听HTTP状态码
                        let result = xhr.responseText //接收数据
                        console.log(result);
                        if (result == "success") {
                            alert("绑定关系" + connection_id + "已删除")
                            location.reload();
                        } else {
                            alert(result);
                        }
                    }
                }
            };
            xhr.send(JSON.stringify(command));
        }
    }

    function diagram_show() {
        window.open("brainMap");
    }

</script>
</html>